package gu.simplemq.jms;

import gu.simplemq.Constant;
import gu.simplemq.pool.BaseMQInstance;

/**
 * redis对象工厂类用于获取producer/consumer,publisher/subscriber,table对象<br>
 * 应用程序结束时会自动清除所有subscriber和consumer
 * @author guyadong
 *
 */
public class JmsFactory implements Constant {
	private final JmsPoolLazys jmsPoolLazys;
	public JmsFactory(JmsPoolLazys jmsPoolLazys) {
		this.jmsPoolLazys = jmsPoolLazys;
	}
	private  final BaseMQInstance<JmsConsumer,JmsPoolLazy> CONSUMERS = 
			new BaseMQInstance<JmsConsumer,JmsPoolLazy>(){
		@Override
		protected void beforeDelete(JmsConsumer r) {
			try {
				r.close();
			} catch (Throwable e) {
				e.printStackTrace();
			}
	}};
	private final BaseMQInstance<JmsSubscriber,JmsPoolLazy> SUBSCRIBERS = 
			new BaseMQInstance<JmsSubscriber,JmsPoolLazy>(){
		@Override
		protected void beforeDelete(JmsSubscriber r) {
			try {
				r.close();
			} catch (Throwable e) {
				e.printStackTrace();
			}
	}};
	private final BaseMQInstance<JmsProducer,JmsPoolLazy> PRODUCERS = 
			new BaseMQInstance<JmsProducer,JmsPoolLazy>(){
		@Override
		protected void beforeDelete(JmsProducer r) {
			r.close();
	}};
	private final BaseMQInstance<JmsPublisher,JmsPoolLazy> PUBLISHERS = 
			new BaseMQInstance<JmsPublisher,JmsPoolLazy>(){
		@Override
		protected void beforeDelete(JmsPublisher r) {
			r.close();
		}};
	/**
	 * 返回 {@link JmsPoolLazy}对应的{@link JmsConsumer}实例
	 * @param jedisPoolLazy
	 * @return
	 * @see gu.simplemq.pool.BaseMQInstance#getInstance(gu.simplemq.jms.JmsPoolLazy.ActivemqPoolLazy)
	 */
	public JmsConsumer getConsumer(JmsPoolLazy jedisPoolLazy) {
		return CONSUMERS.getInstance(jedisPoolLazy);
	}
	
	/** 
	 * 返回{@link JmsPoolLazy}默认实例对应的{@link JmsConsumer}实例
	 * @see  {@link JmsPoolLazy#getDefaultInstance()}
	 */
	public JmsConsumer getConsumer() {
		return CONSUMERS.getInstance(jmsPoolLazys.getDefaultInstance());
	}
	/**
	 * 返回 {@link JmsPoolLazy}对应的{@link JmsSubscriber}实例
	 * @param jedisPoolLazy
	 * @return
	 * @see gu.simplemq.pool.BaseMQInstance#getInstance(gu.simplemq.jms.JmsPoolLazy.ActivemqPoolLazy)
	 */
	public JmsSubscriber getSubscriber(JmsPoolLazy jedisPoolLazy) {
		return SUBSCRIBERS.getInstance(jedisPoolLazy);
	}
	/** 
	 * 返回{@link JmsPoolLazy}默认实例对应的{@link JmsSubscriber}实例
	 * @see  {@link JmsPoolLazy#getDefaultInstance()}
	 */
	public JmsSubscriber getSubscriber() {
		return SUBSCRIBERS.getInstance(jmsPoolLazys.getDefaultInstance());
	}
	/**
	 * 返回 {@link JmsPoolLazy}对应的{@link JmsProducer}实例
	 * @param jedisPoolLazy
	 * @return
	 * @see gu.simplemq.pool.BaseMQInstance#getInstance(gu.simplemq.jms.JmsPoolLazy.ActivemqPoolLazy)
	 */
	public JmsProducer getProducer(JmsPoolLazy jedisPoolLazy) {
		return PRODUCERS.getInstance(jedisPoolLazy);
	}
	/** 
	 * 返回{@link JmsPoolLazy}默认实例对应的{@link JmsProducer}实例
	 * @see  {@link JmsPoolLazy#getDefaultInstance()}
	 */
	public JmsProducer getProducer() {
		return PRODUCERS.getInstance(jmsPoolLazys.getDefaultInstance());
	}
	/**
	 * 返回 {@link JmsPoolLazy}对应的{@link JmsPublisher}实例
	 * @param jedisPoolLazy
	 * @return
	 * @see gu.simplemq.pool.BaseMQInstance#getInstance(gu.simplemq.jms.JmsPoolLazy.ActivemqPoolLazy)
	 */
	public JmsPublisher getPublisher(JmsPoolLazy jedisPoolLazy) {
		return PUBLISHERS.getInstance(jedisPoolLazy);
	}
	/** 
	 * 返回{@link JmsPoolLazy}默认实例对应的{@link JmsPublisher}实例
	 * @see  {@link JmsPoolLazy#getDefaultInstance()}
	 */
	public JmsPublisher getPublisher() {
		return PUBLISHERS.getInstance(jmsPoolLazys.getDefaultInstance());
	}
	/**
	 * 关闭并删除所有consumer,subscriber,producer
	 */
	public synchronized void closeAll(){
		CONSUMERS.clearInstances();
		SUBSCRIBERS.clearInstances();
		PUBLISHERS.clearInstances();
		PRODUCERS.clearInstances();
	}
	{
		// JVM 结束时自动清除所有consumer和subscriber,producer对象
		Runtime.getRuntime().addShutdownHook(new Thread(){

			@Override
			public void run() {
				try {
					AdvisoryMessageManager.closeAll();
					closeAll();
					// 毁连接池对象
					jmsPoolLazys.closeAll();
				} catch (Throwable e) {
					logger.error(e.toString());
				}
			}			
		});
	}
}
